//
//  SettlementAllViewController.m
//  何五路
//
//  Created by _ADY on 15/7/3.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "SettlementAllViewController.h"

@interface SettlementAllViewController ()

@end
#define settlementhight  192/2
@implementation SettlementAllViewController
@synthesize dic,mTableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"商品清单";
        self.view.backgroundColor = bgColor;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenMySize.size.width, screenMySize.size.height)];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.sectionHeaderHeight = 0.0;
    mTableView.sectionFooterHeight = 0.0;
    mTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mTableView];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
    [mTableView setTableFooterView:v];

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (dic)
    {
        int TitInt = 0;
        for (int i = 0; i < dic.count; i ++)
        {
            NSString *d = [NSString stringWithFormat:@"%@",[[dic objectAtIndex:i] objectForKey:@"Quantity"]];
            TitInt = TitInt + [d intValue];
        }
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"共%d项",TitInt] style:UIBarButtonItemStylePlain target:nil action:nil];
        [mTableView reloadData];
    }
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dic.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return settlementhight;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SettlementAllCell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        aTableView.showsVerticalScrollIndicator = NO;
//        cell.tintColor = [UIColor colorWithRed:243/255.0 green:125/255.0 blue:125/255.0 alpha:1];//勾选颜色
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    cell.accessoryType =  UITableViewCellAccessoryNone;
    cell.backgroundColor = [UIColor clearColor];
  
    int Hight = 0;
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,Hight+10, settlementhight-20,settlementhight-20)];
    [bgImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[[dic objectAtIndex:indexPath.row] objectForKey:@"Picture"]objectForKey:@"ImageUrl"]]]];
    bgImageView.contentMode =  UIViewContentModeScaleAspectFit;
    //    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    [cell.contentView addSubview:bgImageView];
    
    UIImageView *bg1ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(9.5,Hight+9.5, settlementhight-19,settlementhight-19)];
    [bg1ImageView setImage:[UIImage imageNamed:@"gwckk"]];
    [cell.contentView addSubview:bg1ImageView];
    
    [cell.contentView addSubview:[self setFrame:CGRectMake(settlementhight, Hight+10, screenMySize.size.width-settlementhight-50, 50) setTitle:[NSString stringWithFormat:@"%@",[[dic objectAtIndex:indexPath.row] objectForKey:@"ProductName"]] setFont:labelSize setColor:[UIColor blackColor]]];
    
    [cell.contentView addSubview:[self setFrame:CGRectMake(settlementhight, Hight+10+50, screenMySize.size.width-settlementhight-50, 20) setTitle:[NSString stringWithFormat:@"%@",[[dic objectAtIndex:indexPath.row] objectForKey:@"CustomerDiscountUnitPrice"]] setFont:labelSize setColor:[UIColor redColor]]];
    
    
    [cell.contentView addSubview:[self setFrame:CGRectMake(screenMySize.size.width-50, settlementhight/2-10, 50, 20) setTitle:[NSString stringWithFormat:@"x%@",[[dic objectAtIndex:indexPath.row] objectForKey:@"Quantity"]] setFont:labelSize setColor:[UIColor blackColor]]];
    
    // Configure the cell...
    return cell;
}

-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title setFont:(int)font setColor:(UIColor*)myColor
{
    title = [title stringByReplacingOccurrencesOfString:@"<null>" withString:@""];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 0;
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:font];
    titleLabel.textColor = myColor;
    return titleLabel;
}

- (void)tableView:(UITableView *)table1View didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsDetailsViewController *details = [[GoodsDetailsViewController alloc] init];
    details.productId = [NSString stringWithFormat:@"%@",[[dic objectAtIndex:indexPath.row] objectForKey:@"ProductId"]];
    details.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:details animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
